package com.mgm.offer.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mgm.advertiser.service.AdvertiserService;
import com.mgm.kafka.service.KafkaProducerService;
import com.mgm.offer.service.PullOfferService;
import com.mgm.pojo.Advertiser;
import com.mgm.utils.HttpConnectionUtil;

@Service
public class PullOfferServiceImpl implements PullOfferService{
	
	private static Logger logger =LoggerFactory.getLogger(PullOfferServiceImpl.class);

	@Autowired
	private AdvertiserService advertiserService;
	
	@Autowired
	private KafkaProducerService kafkaProducerService;
	
	@Override
	public void getOffer() {
		
		List<Advertiser> adlist= advertiserService.findAllNetByEnabled(); //所有开启的广告主
		for (Advertiser advertiser : adlist) {
			//拉取
			String json = HttpConnectionUtil.doGet(advertiser.getUrl());
//			String json = returnCheetahTestJson(); //测试json
			//日志记录
			logger.debug("【Offer下拉】Pull json data : {} == {}", advertiser.getUrl(), json);
			String topic = advertiser.getAdvId()+"_"+advertiser.getId();
			//入kafka
			logger.debug("【pull offer】 send message... topic {}, json {}", topic, json);
			logger.info("【pull offer】 url is {},  topic is {}", advertiser.getUrl(), topic);
			kafkaProducerService.sendMessage(topic, json);
			logger.info("【pull offer】 sended......");
		}
		
	}
	
	private String returnCheetahTestJson() {
		String str = "{\r\n" + 
				"	\"status_code\": 200,\r\n" + 
				"	\"data\": {\r\n" + 
				"		\"campaigns\": [{\r\n" + 
				"			\"id\": \"10861274\",\r\n" + 
				"			\"name\": \"Campaign_21635178_2_Real Madrid App\",\r\n" + 
				"			\"pkg_name\": \"com.mcentric.mcclient.MyMadrid\",\r\n" + 
				"			\"exclude\": \"\",\r\n" + 
				"			\"description\": \"\",\r\n" + 
				"			\"preview_url\": \"https://play.google.com/store/apps/details?id=com.mcentric.mcclient.MyMadrid\",\r\n" + 
				"			\"adSets\": [{\r\n" + 
				"				\"id\": \"10861276\",\r\n" + 
				"				\"name\": \"Adset_21635178_Real Madrid App\",\r\n" + 
				"				\"min_os_version\": \"\",\r\n" + 
				"				\"network\": \"0\",\r\n" + 
				"				\"platform\": \"2\",\r\n" + 
				"				\"creatives\": [{\r\n" + 
				"					\"id\": \"10861277\",\r\n" + 
				"					\"name\": \"Ad_21635178_Real Madrid App\",\r\n" + 
				"					\"icon\": \"https://lh3.googleusercontent.com/ZWmuJuXIKQ4jWvZniwhVci-VMUliBQbTnQ1kM3nU3tAdfj4R_WgPrL61JlFwjK39Nw=s180\",\r\n" + 
				"					\"images\": [{\r\n" + 
				"						\"width\": \"\",\r\n" + 
				"						\"height\": \"\",\r\n" + 
				"						\"format\": \"\",\r\n" + 
				"						\"url\": \"\",\r\n" + 
				"						\"creative_name\": \"\"\r\n" + 
				"					}],\r\n" + 
				"					\"click_url\": \"http://tracking.adkmob.com/click/11038/10861277/?tid={transaction_id}&gaid={advertising_id}&aid={android_id}&sub1={sub1}&sub2={sub2}&sub3={sub3}&sub4={sub4}&sub5={sub5}&sub6={sub6}&pub_id={pub_id}&sub_id={sub_id}&bundle={bundle}&creative_name={creative_name}&per={permission}&flag=2fb62edf054d814e17965ed06870f75d\",\r\n" + 
				"					\"impression_urls\": [\"https://ssdk.adkmob.com/rp/?pgP9ujYkFhqhu-qsIycqniuYIjYzFhPs5HDhmLFW5HczPWms&ac=50&plan_id=10861274&pkg_name=com.mcentric.mcclient.MyMadrid&agid=10861276&mt_id=10861277&app_show_type=839&res_type=341&uid=16082&media_id=3259&posid=3259100&publisher_id=11038&gaid={advertising_id}&android_id={android_id}&sub1={sub1}&sub2={sub2}&sub3={sub3}&sub4={sub4}&sub5={sub5}&sub6={sub6}&pub_id={pub_id}&sub_id={sub_id}&bundle={bundle}&creative_name={creative_name}&per={permission}&arg_redirect=\"]\r\n" + 
				"				}],\r\n" + 
				"				\"cappings\": [{\r\n" + 
				"					\"country\": \"UM\",\r\n" + 
				"					\"daily_cap\": \"649999.35\",\r\n" + 
				"					\"daily_install\": \"12999987\",\r\n" + 
				"					\"pay_out\": \"0.05\"\r\n" + 
				"				}]\r\n" + 
				"			}]\r\n" + 
				"		}, {\r\n" + 
				"			\"id\": \"13678608\",\r\n" + 
				"			\"name\": \"Campaign_22335529_2_Legacy of Discord-Furi\",\r\n" + 
				"			\"pkg_name\": \"com.gtarcade.lod\",\r\n" + 
				"			\"exclude\": \"\",\r\n" + 
				"			\"description\": \"\",\r\n" + 
				"			\"preview_url\": \"https://play.google.com/store/apps/details?id=com.gtarcade.lod\",\r\n" + 
				"			\"adSets\": [{\r\n" + 
				"				\"id\": \"13678610\",\r\n" + 
				"				\"name\": \"Adset_22335529_Legacy of Discord-Furi\",\r\n" + 
				"				\"min_os_version\": \"\",\r\n" + 
				"				\"network\": \"0\",\r\n" + 
				"				\"platform\": \"2\",\r\n" + 
				"				\"creatives\": [{\r\n" + 
				"					\"id\": \"13678612\",\r\n" + 
				"					\"name\": \"Ad_22335529_Legacy of Discord-Furi\",\r\n" + 
				"					\"icon\": \"https://lh3.googleusercontent.com/UGvoykIb4ajxioUycVsopKCjKXZLCJdMap06vviMXrctiqnnjeHvQDXh6GZ8pTj7UA=s180\",\r\n" + 
				"					\"images\": [{\r\n" + 
				"						\"width\": \"\",\r\n" + 
				"						\"height\": \"\",\r\n" + 
				"						\"format\": \"\",\r\n" + 
				"						\"url\": \"\",\r\n" + 
				"						\"creative_name\": \"\"\r\n" + 
				"					}],\r\n" + 
				"					\"click_url\": \"http://tracking.adkmob.com/click/11038/13678612/?tid={transaction_id}&gaid={advertising_id}&aid={android_id}&sub1={sub1}&sub2={sub2}&sub3={sub3}&sub4={sub4}&sub5={sub5}&sub6={sub6}&pub_id={pub_id}&sub_id={sub_id}&bundle={bundle}&creative_name={creative_name}&per={permission}&flag=2fb62edf054d814e17965ed06870f75d\",\r\n" + 
				"					\"impression_urls\": [\"https://ssdk.adkmob.com/rp/?pgP9ujYkFhqhu-qsIycqniuYIjYzFhPs5HDhmLFW5HczPWms&ac=50&plan_id=13678608&pkg_name=com.gtarcade.lod&agid=13678610&mt_id=13678612&app_show_type=839&res_type=341&uid=16082&media_id=3259&posid=3259100&publisher_id=11038&gaid={advertising_id}&android_id={android_id}&sub1={sub1}&sub2={sub2}&sub3={sub3}&sub4={sub4}&sub5={sub5}&sub6={sub6}&pub_id={pub_id}&sub_id={sub_id}&bundle={bundle}&creative_name={creative_name}&per={permission}&arg_redirect=\"]\r\n" + 
				"				}],\r\n" + 
				"				\"cappings\": [{\r\n" + 
				"					\"country\": \"TH\",\r\n" + 
				"					\"daily_cap\": \"649999.35\",\r\n" + 
				"					\"daily_install\": \"5416661\",\r\n" + 
				"					\"pay_out\": \"0.12\"\r\n" + 
				"				}]\r\n" + 
				"			}]\r\n" + 
				"		}]\r\n" + 
				"	},\r\n" + 
				"	\"message\": \"OK\"\r\n" + 
				"}";
		return str;
	}

	private String returnClickdealerTestJson() {
    	String str = "{\r\n" + 
    			"	\"status\": \"OK\",\r\n" + 
    			"	\"ads\": [{\r\n" + 
    			"		\"payout_type\": \"cpi\",\r\n" + 
    			"		\"payout\": 0.14,\r\n" + 
    			"		\"adtype\": \"native\",\r\n" + 
    			"		\"offerid\": 29013123,\r\n" + 
    			"		\"appid\": \"com.iran.ikpayment.app\",\r\n" + 
    			"		\"title\": \"پات\",\r\n" + 
    			"		\"description\": \"پات‫-‬اپلیکیشن پات محصول شرکت کارت اعتباری ایران کیش در حوزه صنعت پرداخت است \",\r\n" + 
    			"		\"restrictions\": \"KPI: The advertiser requires sign up rate > 30% (soft KPI)\\r\\n\\r\\n0 results might lead to deduction.\\r\\n\\r\\nDo not send: Adult, Push, email, search, sms,  Do not re broke the offer!\",\r\n" + 
    			"		\"icon\": \"https://mobair-cdnv1.akamaized.net/logo/d1e01901a5c94fd8ebdba879a25cd78aa1c3f478.png\",\r\n" + 
    			"		\"apprating\": 5.0,\r\n" + 
    			"		\"appcategory\": \"Productivity\",\r\n" + 
    			"		\"incent\": \"no\",\r\n" + 
    			"		\"gaid_idfa_require\": \"no\",\r\n" + 
    			"		\"countries\": \"IR\",\r\n" + 
    			"		\"cities\": [],\r\n" + 
    			"		\"device_types\": [],\r\n" + 
    			"		\"include_min_os_version\": [\"4.0.3\"],\r\n" + 
    			"		\"blacklist_affsub\": {\r\n" + 
    			"			\"affsub1\": []\r\n" + 
    			"		},\r\n" + 
    			"		\"paid_action\": {\r\n" + 
    			"			\"type\": \"Install\",\r\n" + 
    			"			\"multiple_conversions\": false,\r\n" + 
    			"			\"event_id\": 26484,\r\n" + 
    			"			\"event_name\": \"Install\",\r\n" + 
    			"			\"event_description\": \"Install\"\r\n" + 
    			"		},\r\n" + 
    			"		\"clickurl\": \"https://apptrknow.com/dir/redirect?placement_id=10219&campaign_id=29013123&affid=8732&cid=[cid]&data1=[data1]&data2=[data2]&data3=[data3]&data4=[data4]&affsub1=[affsub1]&device_id=[device_id]&idfa=[idfa]&gaid=[gaid]&source_app=[source_app]\",\r\n" + 
    			"		\"tracks\": {\r\n" + 
    			"			\"impression\": \"https://apptrknow.com/api/v1/imp?placement_id=10219&campaign_id=29013123&p=8732\"\r\n" + 
    			"		}\r\n" + 
    			"	},\r\n" + 
    			"	{\r\n" + 
    			"		\"payout_type\": \"cpi\",\r\n" + 
    			"		\"payout\": 0.14,\r\n" + 
    			"		\"adtype\": \"native\",\r\n" + 
    			"		\"offerid\": 29013124,\r\n" + 
    			"		\"appid\": \"com.iran.ikpayment.app\",\r\n" + 
    			"		\"title\": \"پات1\",\r\n" + 
    			"		\"description\": \"پات‫-‬اپلیکیشن پات محصول شرکت کارت اعتباری ایران کیش در حوزه صنعت پرداخت است \",\r\n" + 
    			"		\"restrictions\": \"KPI: The advertiser requires sign up rate > 30% (soft KPI)\\r\\n\\r\\n0 results might lead to deduction.\\r\\n\\r\\nDo not send: Adult, Push, email, search, sms,  Do not re broke the offer!\",\r\n" + 
    			"		\"icon\": \"https://mobair-cdnv1.akamaized.net/logo/d1e01901a5c94fd8ebdba879a25cd78aa1c3f478.png\",\r\n" + 
    			"		\"apprating\": 5.0,\r\n" + 
    			"		\"appcategory\": \"Productivity\",\r\n" + 
    			"		\"incent\": \"no\",\r\n" + 
    			"		\"gaid_idfa_require\": \"no\",\r\n" + 
    			"		\"countries\": \"IR\",\r\n" + 
    			"		\"cities\": [],\r\n" + 
    			"		\"device_types\": [],\r\n" + 
    			"		\"include_min_os_version\": [\"4.0.3\"],\r\n" + 
    			"		\"blacklist_affsub\": {\r\n" + 
    			"			\"affsub1\": []\r\n" + 
    			"		},\r\n" + 
    			"		\"paid_action\": {\r\n" + 
    			"			\"type\": \"Install\",\r\n" + 
    			"			\"multiple_conversions\": false,\r\n" + 
    			"			\"event_id\": 26485,\r\n" + 
    			"			\"event_name\": \"Install\",\r\n" + 
    			"			\"event_description\": \"Install\"\r\n" + 
    			"		},\r\n" + 
    			"		\"clickurl\": \"https://apptrknow.com/dir/redirect?placement_id=10218&campaign_id=29013123&affid=8732&cid=[cid]&data1=[data1]&data2=[data2]&data3=[data3]&data4=[data4]&affsub1=[affsub1]&device_id=[device_id]&idfa=[idfa]&gaid=[gaid]&source_app=[source_app]\",\r\n" + 
    			"		\"tracks\": {\r\n" + 
    			"			\"impression\": \"https://apptrknow.com/api/v1/imp?placement_id=10219&campaign_id=29013123&p=8732\"\r\n" + 
    			"		}\r\n" + 
    			"	}\r\n" + 
    			"	]\r\n" + 
    			"}";
    	return str;
    }
}
